package com.crm.custinfo.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.crm.custinfo.service.impl.UserInfoComm;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.crm.custinfo.domain.CustContractInfo;
import com.crm.custinfo.service.ICustContractInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-23
 */
@RestController
@RequestMapping("/custinfo/contractInfo")
public class CustContractInfoController extends BaseController
{
    @Autowired
    private ICustContractInfoService custContractInfoService;
    @Autowired
    private UserInfoComm userInfoComm;
    @Autowired
    private ISysDeptService isysDeptService;

    /**
     * 查询合同管理列表
     */
    @PreAuthorize("@ss.hasPermi('custinfo:contractInfo:list')")
    @GetMapping("/list")
    public AjaxResult list(CustContractInfo custContractInfo)
    {
        startPage();
        //获取登录账号的角色信息
        Map<String, Object> roleMap = userInfoComm.getUserRoleCode();
        Map<String, Object> result=new HashMap<String, Object>();
        long selectOrgId = custContractInfo.getOrgId() == null || custContractInfo.getOrgId() == -1L ? getDeptId() : custContractInfo.getOrgId();

        int isEmp = MapUtils.getIntValue(roleMap, "is_emp", 0);
        if(isEmp == 1){
            //业务员只能看自己
            custContractInfo.setOrgId(getDeptId());
            custContractInfo.setEmpId(getUserId());
        }
        List<SysDept> deptList = isysDeptService.selectChildrenAllDeptById(selectOrgId);
        Map<String, Object> ids_map = userInfoComm.getDeptIdsEmpIdsByDepts(deptList);
        Long[] deptIds = (Long[]) MapUtils.getObject(ids_map, "deptIds");
        custContractInfo.setOrgIds(deptIds);

        List<CustContractInfo> list = custContractInfoService.selectCustContractInfoList(custContractInfo);
        result.put("isEmp",isEmp);
        result.put("list",getDataTable(list));
        return AjaxResult.success(result);
    }

    /**
     * 导出合同管理列表
     */
    @PreAuthorize("@ss.hasPermi('custinfo:contractInfo:export')")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustContractInfo custContractInfo)
    {
        List<CustContractInfo> list = custContractInfoService.selectCustContractInfoList(custContractInfo);
        ExcelUtil<CustContractInfo> util = new ExcelUtil<CustContractInfo>(CustContractInfo.class);
        util.exportExcel(response, list, "合同管理数据");
    }

    /**
     * 获取合同管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('custinfo:contractInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(custContractInfoService.selectCustContractInfoById(id));
    }

    /**
     * 新增合同管理
     */
    @PreAuthorize("@ss.hasPermi('custinfo:contractInfo:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustContractInfo custContractInfo)
    {
        custContractInfo.setCreateTime(new Date());
        custContractInfo.setCreateBy(String.valueOf(getUserId()));
        return toAjax(custContractInfoService.insertCustContractInfo(custContractInfo));
    }

    /**
     * 修改合同管理
     */
    @PreAuthorize("@ss.hasPermi('custinfo:contractInfo:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustContractInfo custContractInfo)
    {
        custContractInfo.setUpdateBy(String.valueOf(getUserId()));
        custContractInfo.setUpdateTime(new Date());
        return toAjax(custContractInfoService.updateCustContractInfo(custContractInfo));
    }

    /**
     * 删除合同管理
     */
    @PreAuthorize("@ss.hasPermi('custinfo:contractInfo:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(custContractInfoService.deleteCustContractInfoByIds(ids));
    }
}
